package com.bit.forum.service;

import com.bit.forum.common.AppResult;
import com.bit.forum.common.ResultCode;
import com.bit.forum.dao.BoardMapper;
import com.bit.forum.exception.ApplicationException;
import com.bit.forum.model.Board;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class BoardServiceImpl implements IBoardService{

    @Resource
    private BoardMapper boardMapper;
    @Override
    public List<Board> selectByNum(Integer num) {
        //非空校验
        if(num<=0){
            log.info(ResultCode.FAILED_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_NOT_EXISTS));
        }
        List<Board> boards = boardMapper.selectByNum(num);
        return boards;
    }
    @Override
    public Board selectById(Long id) {
        if(id==null||id<=0){
            log.info(ResultCode.FAILED_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_NOT_EXISTS));
        }
        Board board = boardMapper.selectByPrimaryKey(id);
        if(board==null){
            log.info(ResultCode.FAILED_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_NOT_EXISTS));
        }
        return board;
    }

    @Override
    public void addOneArticleCountById(Long id) {
        if(id==null||id<=0){
            log.info(ResultCode.ERROR_BOARD_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_BOARD_NOT_EXISTS));
        }
        Board board = boardMapper.selectByPrimaryKey(id);
        if(board==null){
            log.info(ResultCode.ERROR_BOARD_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_BOARD_NOT_EXISTS));
        }
        Board newBoard = new Board();
        newBoard.setId(id);
        newBoard.setArticleCount(board.getArticleCount()+1);
        int row = boardMapper.updateByPrimaryKeySelective(newBoard);
        if(row<=0){
            log.info(ResultCode.FAILED.toString()+"板块:"+board.getName()+"新增失败");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }
        log.info(ResultCode.SUCCESS+"新增板块:"+board.getName()+"文章新增成功");
    }

    @Override
    public void subOneArticleCountById(Long id) {
        if(id==null||id<=0){
            log.info(ResultCode.ERROR_BOARD_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_BOARD_NOT_EXISTS));
        }
        Board board = boardMapper.selectByPrimaryKey(id);
        if(board==null){
            log.info(ResultCode.ERROR_BOARD_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_BOARD_NOT_EXISTS));
        }
        Board newBoard = new Board();
        newBoard.setId(id);
        newBoard.setArticleCount(board.getArticleCount()-1);
        // 判断减1之后是否小于0
        if (newBoard.getArticleCount() < 0) {
            // 如果小于0那么设置为0
            newBoard.setArticleCount(0);
        }
        int row = boardMapper.updateByPrimaryKeySelective(newBoard);
        if(row<=0){
            log.info(ResultCode.FAILED.toString()+"板块:"+board.getName()+"删除失败");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }
        log.info(ResultCode.SUCCESS+"新增板块:"+board.getName()+"文章删除成功");
    }
}
